[20251218] BOJ / P5 / Lights / 권혁준 #1707
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/5998
🧭 풀이 시간
20분
👀 체감 난이도
✏️ 문제 설명
정점 N개 간선 M개인 그래프가 주어지고, 각 정점에는 전구와 스위치가 하나씩 있다.
처음에 모든 전구는 꺼져있고, 정점에 있는 스위치를 누르면 해당 정점을 포함하여 인접한 모든 정점의 전구 상태가 바뀐다. (ON <-> OFF)
모든 전구를 켜기 위해 눌러야 하는 스위치의 최소 개수를 구해보자.
🔍 풀이 방법
전구들의 상태를
비트마스킹으로 관리한다. 비트가 0이면 꺼져있는 상태, 1이면 켜져있는 상태i번 정점의 스위치를 눌렀을 때는 i번 정점을 포함하여 인접한 정점들의 비트를 반전(XOR)시키면 된다.
전구를 같은 크기의 두 그룹으로 나누어서, 각 그룹에서 전구를 켜고 끄는 모든 경우의 수에 대한
비트마스킹의 집합을 구하고 각각 A, B라고 하자.A에 p라는
비트마스킹이 존재한다면, B에는 ((1<<N)-1) ^ p라는비트마스킹이 존재해야 한다.-> B를
Map으로 관리하면 빠르게 구할 수 있다.A를 반복문으로 돌리면서 B에서 가능한 경우를 찾아내고, 모든 경우에 대한 스위치 작동횟수의 최솟값을 구했다.
⏳ 회고
ez